<?php namespace App\Controllers\Admin;

// =====================================================================
// USERS METHODS
// =====================================================================

class UsersController extends AdminController {

    // ---------------------------------------------------------------------        
    // Only superusers handle users, so we check that on the __construct
    // ---------------------------------------------------------------------        
    public function __construct() {
        parent::__construct();
        // if user is not a superuser then go to other place!!!
        if(!$this->superuser) {
            return \Redirect::to('admin/dashboard');
            exit();
        }
    }
       
    // ---------------------------------------------------------------------        
    // index, list all users of the admin.
    // ---------------------------------------------------------------------        
    public function index() {
        $this->layout->content = \View::make('admin.users.index')->with(array('users'=> \Sentry::findAllUsers()));
    }
    
    // ---------------------------------------------------------------------        
    // create, we show the form to create users.
    // ---------------------------------------------------------------------        
    public function create() {
        $this->layout->content = \View::make('admin.users.abm');
    }

    // ---------------------------------------------------------------------        
    // store, is the function to save information of users into DB.
    // ---------------------------------------------------------------------            
    public function store() {
        // Add some validation
	    $rules = array(
	        'email' => 'required|email|unique:users',
	        'password' => 'required|confirmed',
	        'password_confirmation' => 'required'
	    );
	    
        // Get the submitted data
	    $input = \Input::get();

        // Validate
	    $validation = \Validator::make($input, $rules);
	    if ($validation->fails()) return \Redirect::to('admin/users/create')->withInput()->withErrors($validation);
        
        // Create user 
	    $user = \Sentry::createUser(array(
	        'email'     => \Input::get('email'),
	        'password'  => \Input::get('password'),
	        'activated' => true
	    ));    
	    $user->first_name = \Input::get('first_name');
	    $user->last_name = \Input::get('last_name');
        $user->permissions = array('superuser' => \Input::get('superuser')=='Y' ? 1 : 0);
	    $user->update();

        //Add user to the administration group (we are going this to handle by BO).
	    $adminGroup = \Sentry::findGroupByName("administration");
        $user->addGroup($adminGroup);
        
		return \Redirect::to('admin/users')->with('success','User has been added successfully.');
	}
	
    // ---------------------------------------------------------------------        
    // edit, is the function to show the user form to edit.
    // ---------------------------------------------------------------------        
    public function edit($id=null) {
    
        $user = \Sentry::findUserById($id);       
        $this->layout->content = \View::make('admin.users.edit')->with(array('user'=> $user, "superuser" => $user->isSuperUser()));
    }

    // ---------------------------------------------------------------------        
    // postUseredit, is the function to edit users.
    // ---------------------------------------------------------------------            
    public function update($id) {        
        try {
			$user = \Sentry::findUserById($id);

			if(\Input::get('password')!=''){
				$user->password = \Input::get('password');
			}
			$user->first_name = \Input::get('first_name');
			$user->last_name = \Input::get('last_name');
	        $user->permissions = array('superuser' => \Input::get('superuser')=='Y' ? 1 : 0);

			if($user->save()) {
				return \Redirect::to('admin/users')->with('success','User info has been updated successfully');
			} else {
				return \Redirect::to('admin/users')->with('error','Unable to save user');
			}
		}
		catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
		    return \Redirect::to('admin/users')->with('error','Unable to find user');
		}
	}
	
    // ---------------------------------------------------------------------        
    // postUserdelete, is the function to delete an admin user.
    // ---------------------------------------------------------------------        
	public function destroy($id=null) {
		try {
	 		$user = \Sentry::findUserById($id);
			$delete = $user->delete();

		    if ($delete) {
		        return \Redirect::to('admin/users')->with('success', 'User has been deleted successfully.');
		    }
		    else {
		        return \Redirect::to('admin/users')->with('error',"User not deleted, please try again");
		    }
		}
		catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
		    return \Redirect::to('admin/users')->with('error','Unable to find user');
		}
	}

    // ---------------------------------------------------------------------        
    // getUserblock, is the function to block admin access to a user.
    // ---------------------------------------------------------------------            
    public function getBlock($id=null) {

		try {
			$user = \Sentry::findUserById($id);
			$user->activated=false;
			$user->save();
			return \Redirect::to('admin/users')->with('success','User has been blocked successfully.');
		}
		catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
		    return \Redirect::to('admin/users')->with('error','Unable to Block user');
		}

	}

    // ---------------------------------------------------------------------        
    // getUseractive, is the function to restore admin access to a user.
    // ---------------------------------------------------------------------        
	public function getActivate($id=null) {
		try {
			$user = \Sentry::findUserById($id);
			$user->activated=true;
			$user->save();
	        return \Redirect::to('admin/users')->with('success','User has been Activated successfully.');
	    }
	    catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
	        return \Redirect::to('admin/users')->with('error','Unable to Activate user');
	    }

	}    
}
	
